package org.dev.hi.hiui.empty

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.StringRes
import kotlinx.android.synthetic.main.hi_refresh_overview.view.*
import kotlinx.android.synthetic.main.layout_empty_view.view.*
import org.dev.hi.hiui.R

/**
 * Author WenPing
 * CreateTime 2021/4/23.
 * Description:
 */
class EmptyView : LinearLayout {

    private var title: TextView

    private var icon: TextView

    private var desc: TextView

    private var button: Button

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(
        context,
        attributeSet,
        defStyle
    ) {

        orientation = VERTICAL

        gravity = Gravity.CENTER

        LayoutInflater.from(context)
            .inflate(R.layout.layout_empty_view, this, true)

        icon = findViewById(R.id.empty_icon)

        title = findViewById(R.id.empty_title)

        desc = findViewById(R.id.empty_text)

        button = findViewById(R.id.empty_action)
    }

    /**
     * 设置icon，需要在stirng.xml中定义 iconFont.ttf中的uniCode码
     */
    fun setIcon(@StringRes iConRes: Int) {
        icon.setText(iConRes)
    }

    fun setTitle(text: String) {
        title.text = text
        title.visibility = if (TextUtils.isEmpty(text)) View.GONE else View.VISIBLE
    }

    fun setDesc(text: String) {
        desc.text = text
        desc.visibility = if (TextUtils.isEmpty(text)) View.GONE else View.VISIBLE
    }

    @JvmOverloads
    fun setHelpAction(@StringRes actionId: Int = R.string.if_detail, listener: OnClickListener) {
        empty_tips.run {
            setText(actionId)
            setOnClickListener(listener)
            visibility = if (actionId == -1) View.GONE else View.VISIBLE
        }
    }

    fun setButton(text: String, listener: OnClickListener) {
        if (TextUtils.isEmpty(text)) {
            button.visibility = View.GONE
        } else {
            button.run {
                visibility = View.VISIBLE
                setText(text)
                setOnClickListener(listener)
            }
        }
    }
}